Repository navigation
feat: upgrade existing Google Services Gradle plugin version on configure - #479
Conversation
…gure Update `_applyGoogleServicesPlugin` and `_applyGoogleServicesPluginKts` to upgrade older `com.google.gms.google-services` plugin pins forward to `googleServicesGradlePluginVersion` when re-running `flutterfire configure`.
SelaseKay
left a comment
There was a problem hiding this comment.
Hi @AustinBenoit, thanks for the PR. I went through the implementation and found a few edge cases worth addressing.
1. Comments can prevent the actual plugin version from being upgraded
upgradeGoogleServicesPluginVersion currently uses the first matching declaration in the file. However, that match could be inside a comment.
For example, assuming the target version is 4.7.0:
// id "com.google.gms.google-services" version "4.7.0" apply false
id "com.google.gms.google-services" version "4.3.15" apply falseThe helper finds 4.7.0 in the comment, considers the version up to date, and returns without upgrading the actual declaration (4.3.15).
If the commented version is older than the target, the helper could instead end up updating the comment rather than the actual declaration.
Could we ensure that commented-out declarations are ignored and that the helper finds the actual plugin declaration before deciding whether an upgrade is needed?
2. Some valid Gradle declarations are detected but not upgraded
The plugin existence check and the version matching check don't currently accept the same syntax.
For example, in android/settings.gradle.kts:
id("com.google.gms.google-services") version "4.3.15" apply(false)The Kotlin existence check recognises the plugin ID, so no new declaration is added. However, the version matching regex expects apply false, so it doesn't recognise this declaration either.
The same issue occurs with extra whitespace or tabs in the Groovy settings.gradle declaration.
As a result, flutterfire configure completes successfully without upgrading the version.
Could we make the version matching handle these valid syntax variations? Also, if the plugin ID is present but its version cannot be parsed, it would be helpful to print a warning identifying the file and the declaration that couldn't be processed.
3. The target version should come from the current app's resolved firebase_core dependency
Currently, the target version comes from:
getFirebaseCorePubSpec().googleServicesGradlePluginVersiongetFirebaseCorePubSpec() selects the highest firebase_core version available in the local pub cache, rather than the version resolved by the current application.
For example, an app using firebase_core 3.10.0 could end up using the Google Services Gradle plugin version from firebase_core 3.15.0 simply because that version is also present in the cache. This would happen if those two packages declare different Google Services plugin versions.
Although this lookup already exists, this PR passes its result into the new upgrade. That upgrade runs when flutterfire configure takes the latest settings path and the plugin is already declared. On that path, another project's cached firebase_core can change the current project's pin.
Could we instead resolve the version from the firebase_core dependency used by the current app?
The forward-only comparison should remain so that an existing newer plugin version is never downgraded.
This lookup is also used for the Crashlytics and Performance plugin versions, so it would be worth considering those paths when addressing this.
4. The legacy classpath check skips the settings.gradle upgrade
_applyGoogleServicesPlugin returns early when android/app/build.gradle contains com.google.gms:google-services. _applyGoogleServicesPluginKts returns early when android/app/build.gradle.kts contains that text. In both functions the settings upgrade is below that return, so the settings file is left unchanged even when its plugin declaration is older than the target.
For example, a project could have the legacy classpath declaration in android/app/build.gradle alongside a plugin declaration in android/settings.gradle.
Could we ensure the settings declaration is still checked and upgraded when this legacy classpath declaration is present?
5. Additional test coverage
I'd suggest adding coverage for the following cases:
- A commented-out declaration appearing before the actual plugin declaration.
- A valid
apply(false)declaration in Kotlin DSL. - Extra whitespace in a Groovy declaration.
- Selecting the Google Services plugin version from the current app's resolved
firebase_coredependency when multiple versions exist in the pub cache.
These should help ensure the upgrade works correctly across existing project configurations.
- Ignore commented-out declarations when looking for the pin. - Accept `apply(false)`, extra whitespace and parenthesised Groovy declarations, and warn when a declaration's version cannot be read. - Take the target version from the `firebase_core` the app resolves to instead of the highest one in the pub cache. The cache lookup stays as the fallback for adding missing declarations and never rewrites a pin. - Upgrade the settings pin even when `android/app/build.gradle(.kts)` still has the legacy classpath declaration.
|
Thanks for the review @SelaseKay. All five points are addressed in f95a21f:
|
Description
When users re-run
flutterfire configureon an existing Android project whoseandroid/settings.gradleorandroid/settings.gradle.ktsalready declarescom.google.gms.google-services, the existing version pin (for example4.3.15or4.4.4) was left unchanged. Becauseflutter pub upgradedoes not modify Gradle settings files, projects stayed on older plugin versions that lack newer build-time plugin capabilities.Changes
upgradeGoogleServicesPluginVersioninpackages/flutterfire_cli/lib/src/firebase/firebase_android_writes.dart. It moves an existingcom.google.gms.google-servicespin inandroid/settings.gradle(.kts)forward, never downgrades it, and rewrites only the text of the version literal.id "...",id '...',id("...")) and Kotlin (id("...")) declarations withversion "..."orversion("..."), whatever the spacing and whether they end inapply falseorapply(false). Commented-out declarations are ignored.firebase_corethe app resolves to, read from.dart_tool/package_config.json(pub workspaces included) by the newgetResolvedFirebaseCorePubSpec. The Crashlytics and Performance plugin versions use the same lookup. The existing pub cache lookup stays as the fallback when the app's dependencies are not resolved: missing declarations are still added from it, but an existing pin is not rewritten.gradleContentUpdates, ahead of applying the plugin, so it still happens whenandroid/app/build.gradle(.kts)contains the legacycom.google.gms:google-servicesclasspath declaration.gradleContentUpdatestakes an optionallogger, used for the warning.Testing
dart format --output=none --set-exit-if-changed .(no changes)dart analyze --fatal-infos(no issues)dart test test/firebase_android_writes_test.dart(44/44 passed), covering commented-out declarations,apply(false), extra whitespace, unreadable versions, the resolvedfirebase_corelookup with several versions in a pub cache, and the legacy classpath case for both DSLs.Type of Change
feat-- New feature (non-breaking change which adds functionality)fix-- Bug fix (non-breaking change which fixes an issue)!-- Breaking change (fix or feature that would cause existing functionality to change)refactor-- Code refactorci-- Build configuration changedocs-- Documentationchore-- ChorePrepared with AI coding agents (Google-internal, built on Jetski) and opened by @AustinBenoit.